package json

import (
	"encoding/json"
	"errors"
)

type UserInfo struct {
	UserName string `json:"userName"`
	Age      int    `json:"age"`
}

func ToJSONString(data interface{}) (string, error) {
	ret, err := json.Marshal(data)
	if err != nil {
		return "", err
	}
	return string(ret), nil
}

func GetObject(str string) (UserInfo, error) {
	var user UserInfo
	if len(str) == 0 {
		return user, errors.New("string is nil")
	}
	err := json.Unmarshal([]byte(str), &user)
	if err != nil {
		return user, errors.New("unmarshal fail")
	}
	return user, nil
}
